package com.one.del;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.Contacts.People;
import android.util.Log;


public class DelThread extends Thread{
	
	private static Uri URL_SMS = Uri.parse("content://sms");

	
	private Context context;
	
	private List<String> personList = new ArrayList<String>();
	
	private int delItemIndex;
	
	
	
//	private static final String[] TELE_PROJECTION = {
//		CallLog.Calls.TYPE,CallLog.Calls.NUMBER,CallLog.Calls.DATE,CallLog.Calls.DURATION,
//		CallLog.Calls.NEW,CallLog.Calls.CACHED_NAME,
//	};
	
	public DelThread(Context context,List<String> list,int index){
		this.context = context;
		this.personList.addAll(list);
		this.delItemIndex = index;
	}
	
	public void run(){
		ContentResolver content = context.getContentResolver();		
		for(String person:personList){
			Cursor c = content.query(People.CONTENT_URI, null, People.NAME+"=?", new String[]{person}, null);
			if(c.moveToFirst()){
				String number = c.getString(c.getColumnIndex(People.NUMBER));
				del(number);
			}else if(person!=null && !"".equals(person)){
				del(person);
			}
		}
		
	}
	
	private void del(String number){
		ContentResolver content = context.getContentResolver();		
		String selection = CallLog.Calls.NUMBER +" =? or "+CallLog.Calls.NUMBER+"=?";	
		String selection2 = "address =? or address =?";
		String sameNumber = "+86"+number;
		switch(delItemIndex){
			case 0:	
				content.delete(CallLog.Calls.CONTENT_URI, selection, new String[]{number,sameNumber});	
				//清除短信
				content.delete(URL_SMS, selection2, new String[]{number,sameNumber});	
				break;
			case 1:
				content.delete(CallLog.Calls.CONTENT_URI, selection, new String[]{number,sameNumber});	
				break;
			case 2:
				content.delete(URL_SMS, selection2, new String[]{number,sameNumber});	
				break;
		}
		
	}
	
	private List<String> getNumberList(){
		ContentResolver content = context.getContentResolver();		
		List<String> numberList = new ArrayList<String>();
		for(String person:personList){
			Cursor c = content.query(People.CONTENT_URI, null, People.NAME+"=?", new String[]{person}, null);
			if(c.moveToFirst()){
				String number = c.getString(c.getColumnIndex(People.NUMBER));
				numberList.add(number);
			}else if(person!=null && !"".equals(person)){
				numberList.add(person);
			}
			c.close();
		}
		return numberList;
	}
	
	private String getCondition(List<String> numberList){
		if(numberList!= null){
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<numberList.size();i++){
				sb.append(numberList.get(i));
				if(i!= (numberList.size()-1)){
					sb.append(",");
				}
			}
			return sb.toString();
		}
		return "";
	}

}
